package com.bringway.webservice.http.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.BaseJdbcDao;
import com.bringway.webservice.http.dao.MeetingBookingDao;

import net.sf.json.JSONObject;

/*
* @author 作者 xuef
* @version 创建时间：2015年9月16日 下午1:49:26
* 类说明
*/
@Repository
public class MeetingBookingDaoImpl extends BaseJdbcDao implements MeetingBookingDao {

	@Override
	public List<Map<String, Object>> queryMeetingBookingList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit",10);
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		sql.append(" SELECT ID,COMPANY_ID,COMPANY_NAME,COMPANY_JC,BOOKING_USER_NAME,DATE_FORMAT(BOOKING_DATE,'%Y-%m-%d') BOOKING_DATE ");
		sql.append(" ,DATE_FORMAT(BOOKING_START_TIME,'%H:%i:%s') BOOKING_START_TIME,DATE_FORMAT(BOOKING_END_TIME,'%H:%i:%s') BOOKING_END_TIME ");
		sql.append(" ,TEL ");
		sql.append(" FROM T_WS_MEETING_BOOKING ");
		sql.append(" WHERE 1=1 ");
		sql.append(" ORDER BY ID DESC   ");
		sql.append(" LIMIT :START,:LIMIT");
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(),paramMap);
	}

	@SuppressWarnings("deprecation")
	@Override
	public int getMeetingBookingCount(JSONObject params) {
		boolean isValidSave =  params.optBoolean("isValidSave");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		sql.append(" SELECT COUNT(*) CT ");
		sql.append(" FROM T_WS_MEETING_BOOKING ");
		sql.append(" WHERE 1=1 ");
		if(isValidSave){
			String vBookingDate =  params.optString("bookingDate");
			String vBookingStartTime =  params.optString("bookingStartTime");
			String vBookingEndTime =  params.optString("bookingEndTime");
			sql.append(" AND BOOKING_DATE = :vBookingDate");
			paramMap.put("vBookingDate", vBookingDate);
			sql.append(" AND BOOKING_END_TIME > :vBookingStartTime ");
			paramMap.put("vBookingStartTime", vBookingStartTime);
			sql.append(" AND BOOKING_START_TIME < :vBookingEndTime ");
			paramMap.put("vBookingEndTime", vBookingEndTime);
		}
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(),paramMap);
	}

	@Override
	public boolean saveMeetingBooking(JSONObject params) {
		String companyId =  params.optString("companyId");
		String companyName =  params.optString("companyName");
		String companyJc =  params.optString("companyJc");
		String bookingUserName =  params.optString("bookingUserName");
		String bookingDate =  params.optString("bookingDate");
		String bookingStartTime =  params.optString("bookingStartTime");
		String bookingEndTime =  params.optString("bookingEndTime");
		String tel =  params.optString("tel");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		sql.append(" INSERT INTO T_WS_MEETING_BOOKING ");
		sql.append(" (COMPANY_ID,COMPANY_NAME,COMPANY_JC,BOOKING_USER_NAME,BOOKING_DATE,BOOKING_START_TIME,BOOKING_END_TIME,TEL) ");
		sql.append(" VALUES ");
		sql.append(" (:COMPANY_ID,:COMPANY_NAME,:COMPANY_JC,:BOOKING_USER_NAME,:BOOKING_DATE,:BOOKING_START_TIME,:BOOKING_END_TIME,:TEL)  ");
		paramMap.put("COMPANY_ID", companyId);
		paramMap.put("COMPANY_NAME", companyName);
		paramMap.put("COMPANY_JC", companyJc);
		paramMap.put("BOOKING_USER_NAME", bookingUserName);
		paramMap.put("BOOKING_DATE", bookingDate);
		paramMap.put("BOOKING_START_TIME", bookingStartTime);
		paramMap.put("BOOKING_END_TIME", bookingEndTime);
		paramMap.put("TEL", tel);
		int result = this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
		if(result > 0){
			return true;
		}
		return false;
	}
	
}
